package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.qilu.pe.R;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.Patient;
import com.qilu.pe.dao.bean.PatientDetail;
import com.qilu.pe.dao.bean.ProductPatientInfo;
import com.qilu.pe.dao.bean.TestHistory;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.ActiviceCollector;
import com.qilu.pe.support.util.GlideUtil;
import com.qilu.pe.support.util.PeUtil;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.SizeUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class PatientDetailActivity extends BaseActivity {
    private RecyclerView rcv_test_history;// 检验历史
    private RoundedImageView iv_header;
    private TextView tv_name, tv_sex, tv_age, tv_phone, tv_doctor;
    private TextView tv_history_info, tv_special_info;// 病史 特殊信息
    private ImageView iv_sex;

    private TestHistoryListAdapter adapter;
    private List<TestHistory> mTestHistories;

    private Patient patient;// 传过来的病人对象
    private PatientDetail detail;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_patient_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        rcv_test_history = findViewById(R.id.rcv_test_history);
        iv_header = findViewById(R.id.iv_header);
        tv_name = findViewById(R.id.tv_name);
        tv_sex = findViewById(R.id.tv_sex);
        tv_age = findViewById(R.id.tv_age);
        tv_phone = findViewById(R.id.tv_phone);
        tv_doctor = findViewById(R.id.tv_doctor);
        tv_history_info = findViewById(R.id.tv_history_info);
        tv_special_info = findViewById(R.id.tv_special_info);
        iv_sex = findViewById(R.id.iv_sex);

        toolbar.setNavigationIcon(R.mipmap.ic_left_arrow);

        adapter = new TestHistoryListAdapter(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new TestHistoryListAdapter(this);
        rcv_test_history.setLayoutManager(manager);
        rcv_test_history.setAdapter(adapter);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            patient = (Patient) bundle.getSerializable("patient");
        }

        setHeadTitle("患者详情");

        setListeners();

        hideProgress();
        reqPatientDetail();
        reqTestHistory();
        showProgress();
    }

    protected void setListeners() {
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", String.valueOf(mTestHistories.get(position).getId()));
                bundle.putSerializable("patientDetail", detail);
                startActivity(PatientTestItemDetailActivity.class, bundle);
            }
        });
    }

    // 请求病人详情
    private Disposable reqPatientDetail() {
        return APIRetrofit.getDefault().reqPatientInfo(Global.HEADER, patient.getId())
                .compose(RxUtil.<BaseResult2<PatientDetail>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<PatientDetail>>() {
                    @Override
                    public void accept(BaseResult2<PatientDetail> patientDetailResult2) throws Exception {
                        hideProgress();
                        if (patientDetailResult2.isSuccess()) {
                            detail = patientDetailResult2.getData();
                        } else {

                        }
                        if (null != detail) {
                            tv_name.setText(detail.getName());
                            tv_sex.setText(TextUtils.equals("1", patient.getSex()) ? "女" : "男");
                            tv_age.setText("年龄:" + detail.getAge());
                            tv_phone.setText(detail.getPhone());
                            tv_doctor.setText("负责人:" + detail.getU_name());
                            tv_history_info.setText(detail.getMedical());
                            tv_special_info.setText(detail.getRemark());
                            iv_sex.setImageResource(TextUtils.equals("1", patient.getSex()) ?
                                    R.mipmap.ic_patient_sex_female : R.mipmap.ic_patient_sex_male_2);// ic_patient_sex_female ic_patient_sex_male
                            GlideUtil.loadImg(detail.getPicture(), iv_header);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    // 请求项目信息详情 检验历史
    private Disposable reqTestHistory() {
        return APIRetrofit.getDefault().getTestHistory(Global.HEADER, 1, 999999, Integer.parseInt(patient.getId()))
                .compose(RxUtil.<BaseResult2<List<TestHistory>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<TestHistory>>>() {
                    @Override
                    public void accept(BaseResult2<List<TestHistory>> res) throws Exception {
                        hideProgress();
                        if (res.isSuccess()) {
                            mTestHistories = res.getData();
                            adapter.setDataList(mTestHistories);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class TestHistoryListAdapter extends ListBaseAdapter<TestHistory> {

        public TestHistoryListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_test_history_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            RelativeLayout rl_item = holder.getView(R.id.rl_item);
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_state = holder.getView(R.id.tv_state);
            TextView tv_num = holder.getView(R.id.tv_num);
            TextView tv_date = holder.getView(R.id.tv_date);
            TextView tv_detail = holder.getView(R.id.tv_detail);

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rl_item.getLayoutParams();
            if (position == 0) {
                params.topMargin = SizeUtils.dp2px(10);
                params.bottomMargin = SizeUtils.dp2px(5);
            } else if (position == mDataList.size() - 1) {
                params.topMargin = SizeUtils.dp2px(5);
                params.bottomMargin = SizeUtils.dp2px(10);
            } else {
                params.topMargin = SizeUtils.dp2px(5);
                params.bottomMargin = SizeUtils.dp2px(5);
            }
            rl_item.setLayoutParams(params);

            TestHistory test = mDataList.get(position);
            tv_name.setText(test.getProject_name());
            tv_state.setText(PeUtil.getOrderStatus(String.valueOf(test.getType())));
            tv_num.setText("共计" + test.getAbnormal() + "项异常");
            tv_date.setText(test.getTime());
        }
    }
}